package org.mspring.platform.utils.http;

import java.io.Serializable;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.annotation.Immutable;
import org.apache.http.util.Args;

/**
 * 可用于文件上传
 *
 * @author Gao Youbo
 * @since 2014-05-22 14:52:50
 */
@Immutable
public class ByteArrayNameValuePair implements NameValuePair, Cloneable, Serializable {

    private final String name;
    private final byte[] fileBytes;
    private String value;

    public ByteArrayNameValuePair(String name, byte[] fileBytes) {
        this.name = Args.notNull(name, "Name");
        this.fileBytes = fileBytes;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getValue() {
        if (this.value == null && fileBytes != null) {
            this.value = Base64.encodeBase64String(fileBytes);
        }
        return this.value;
    }

    public byte[] getFileBytes() {
        return this.fileBytes;
    }
}
